package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.GroupBuyBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.ui.view.CircleImageView;
import com.saifan.wyy_ov.utils.ImgUtil;

public class AdapterGroupBuy extends BaseAdapter {
    Context context;
    List<GroupBuyBean> groupBuyBeans;

    public AdapterGroupBuy(Context context, List<GroupBuyBean> groupBuyBeans) {
        this.context = context;
        this.groupBuyBeans = groupBuyBeans;
    }

    @Override
    public int getCount() {
        if (groupBuyBeans!=null && !groupBuyBeans.isEmpty()) {
            return groupBuyBeans.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (groupBuyBeans!=null && !groupBuyBeans.isEmpty())
            return groupBuyBeans.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<GroupBuyBean> getGroupBuyBeans() {
        return groupBuyBeans;
    }

    public void setGroupBuyBeans(List<GroupBuyBean> groupBuyBeans) {
        this.groupBuyBeans = groupBuyBeans;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final GroupBuyBean groupBuyBean = groupBuyBeans.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.group_buy_view, null);
            viewHolder.ll_all_goods = (LinearLayout) convertView.findViewById(R.id.ll_all_goods);
            viewHolder.ll_store = (LinearLayout) convertView.findViewById(R.id.ll_store);
            viewHolder.tv_group_buy_goods_name = (TextView) convertView.findViewById(R.id.tv_group_buy_goods_name);
            viewHolder.tv_postion = (TextView) convertView.findViewById(R.id.tv_postion);
            viewHolder.tv_read_comments = (TextView) convertView.findViewById(R.id.tv_read_comments);
            viewHolder.lv_group_buy_item = (com.saifan.wyy_ov.ui.view.NoScrollListView) convertView.findViewById(R.id.lv_group_buy_item);
            viewHolder.tv_all_goods = (TextView) convertView.findViewById(R.id.tv_all_goods);
            viewHolder.iv_store_img = (CircleImageView) convertView.findViewById(R.id.iv_store_img);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final AdapterGroupBuyItem adapter = new AdapterGroupBuyItem(context, groupBuyBean, groupBuyBean.getGoodsList());
        viewHolder.tv_group_buy_goods_name.setText(groupBuyBeans.get(position).getStoreName());
        ImgUtil.DisPlayImg(groupBuyBean.getHeadPhoto(), viewHolder.iv_store_img, context);
        if (groupBuyBean.getGoodsList().size() < 3) {
            viewHolder.ll_all_goods.setVisibility(View.GONE);
        } else {
            viewHolder.ll_all_goods.setVisibility(View.VISIBLE);
            viewHolder.tv_all_goods.setText("查看全部 " + groupBuyBean.getGoodsList().size() + " 个商品");
        }
        viewHolder.ll_all_goods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                adapter.setShowCount(groupBuyBean.getGoodsList().size());
                arg0.setVisibility(View.GONE);
            }
        });
        viewHolder.tv_read_comments.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, CommentsActivity.class);
                intent.putExtra("ID", groupBuyBean.getID());
                context.startActivity(intent);
            }
        });
        viewHolder.lv_group_buy_item.setAdapter(adapter);
        viewHolder.lv_group_buy_item.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
                Intent intent = new Intent(context, GroupBuyDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("goods", groupBuyBean.getGoodsList().get(paramInt));
                bundle.putSerializable("groupBuyBean", groupBuyBean);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        viewHolder.ll_store.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, StoreDetailsActivity.class);
                StoreBean storeBean = new StoreBean();
                storeBean.setID(groupBuyBean.getID());
                storeBean.setStoreName(groupBuyBean.getStoreName());
                storeBean.setHeadPhoto(groupBuyBean.getHeadPhoto());
                storeBean.setTel(groupBuyBean.getTel());
                storeBean.setBusinessDate(groupBuyBean.getBusinessDate());
                storeBean.setBusinessTime(groupBuyBean.getBusinessTime());
                storeBean.setAddress(groupBuyBean.getStoreAddress());
                storeBean.setIsDelivery(true);
                intent.putExtra("data", storeBean);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {

        public TextView tv_group_buy_goods_name;
        public TextView tv_postion;
        public TextView tv_read_comments;
        public com.saifan.wyy_ov.ui.view.NoScrollListView lv_group_buy_item;
        public TextView tv_all_goods;
        public LinearLayout ll_all_goods;
        public CircleImageView iv_store_img;
        public LinearLayout ll_store;

    }
}
